package com.example.yusei.ex8;

import android.app.DatePickerDialog;
import android.icu.text.SimpleDateFormat;
import android.icu.util.Calendar;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.TextView;
import android.widget.Toast;

import java.text.ParseException;
import java.util.Date;
import java.util.Locale;

import static android.R.attr.y;

public class MainActivity extends AppCompatActivity {

    private TextView showdate;
    private Button setdate;
    private int year;
    private int month;
    private int day;




    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        showdate = (TextView)this.findViewById(R.id.showdate);
        setdate = (Button)this.findViewById(R.id.setdate);
        Calendar c = Calendar.getInstance(Locale.CHINA);
        Date mydate = new Date();
        c.setTime(mydate);
        year = c.get(Calendar.YEAR);
        month = c.get(Calendar.MONTH);
        day = c.get(Calendar.DAY_OF_MONTH);
        showdate.setText("当前日期："+year+'-'+(month+1)+'-'+day);


        setdate.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                DatePickerDialog myDatePickerDialog=new DatePickerDialog(MainActivity.this,Datelistener,year,month,day);
                myDatePickerDialog.show();
            }
        });

    }




    private DatePickerDialog.OnDateSetListener Datelistener = new DatePickerDialog.OnDateSetListener() {
        @Override
        public void onDateSet(DatePicker v, int y, int m, int d) {
            Calendar c = Calendar.getInstance(Locale.CHINA);
            year = c.get(Calendar.YEAR);
            month = c.get(Calendar.MONTH);
            day = c.get(Calendar.DAY_OF_MONTH);

            SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
            String time =y+"-"+m+"-"+d;
            String current_time = year+"-"+month+"-"+day;
            Date date = null;
            Date current_date = null;

            try {
                date = format.parse(time);
                current_date = format.parse(current_time);
            }catch (ParseException e) {
                e.printStackTrace();
            }

            if(date.getTime() <= current_date.getTime()) {
                year = y;
                month = m;
                day = d;
                updateDate();
            }else {
                Toast.makeText(getApplicationContext(),"设置日期无效", Toast.LENGTH_SHORT).show();
            }

        }
        private void updateDate() {
            showdate.setText("当前日期："+year+"-"+(month+1)+"-"+day);
        }
    };

}
